package com.moonbird.springboot.controller;

import com.moonbird.springboot.DTO.Result;
import com.moonbird.springboot.DTO.SynthesizeRequest;
import com.moonbird.springboot.DTO.faceDTO;
import com.moonbird.springboot.Utils.RecognitionImage.ImageTextRecognitionUtils;
import com.moonbird.springboot.service.*;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

/**
 * 功能接口
 * @author hpl
 */

@RestController
@CrossOrigin
@RequestMapping("/api")
public class SparkController {


    @Autowired
    private FaceService faceService;
    @Autowired
    private ImageService imageService;
    @Autowired
    private AudioService audioService;
    @Autowired
    private PictureRecognitionTextService pictureRecognitionTextService;
    @Autowired
    private PptService pptService;
    @Autowired
    private  ChatService chatService;

    // 聊天接口
    @PostMapping("/chat/message")
    public Result<String> sendMessage(@RequestBody String message) {
        // 解析 JSON 字符串
        JSONObject jsonObject = new JSONObject(message);
        // 获取 "description" 字段的值
        message = jsonObject.getString("message");
        //System.out.println("message:"+message);
        return chatService.sendMessage(message);
    }

    // 人脸分析接口
    @PostMapping("/face/analyze")
    public Result<faceDTO> analyzeFace(@RequestParam("file") MultipartFile file) throws IOException {
        faceDTO result = faceService.analyzeFaceAndGetScore(file);
        System.out.println(Result.success(result));
        return Result.success(result);
    }

    // 图片生成接口
    @PostMapping("/image/generate")
    public Result<String> generateImage(@RequestBody String description) throws IOException {
        String imageurl = imageService.generateImage(description);
        System.out.println(Result.success(imageurl));
        return Result.success(imageurl);
    }

    // 音频转文字接口 语音识别接口
    @PostMapping("/audio/transcribe")
    public Result<String> transcribeAudio(@RequestParam("file") MultipartFile file) throws IOException, InterruptedException {
        System.out.println("11111111");
        System.out.println("================================音频转文字===================================");
        if (file.isEmpty()){
            System.out.println("文件为空");
            return Result.fail("文件为空");
        }
        return audioService.transcribeAudio(file);
    }

    //文字转语音接口
    @PostMapping("/audio/toTTS")
    public Result<String> transcribeAudio(@RequestBody SynthesizeRequest request, HttpServletResponse response) {
        String text = request.getText();
        String vcn = request.getVcn(); // 选择要使用的发音人
        Result result = audioService.toTTS(text, vcn);
        System.out.println(result);
        return result;
    }

    // 图片文字识别转文档接口
    @PostMapping("/image/documents")
    public Result<String> generatedDocuments(@RequestParam("file") MultipartFile file, @RequestParam String type) throws InterruptedException {
        // 生成文件类型：0 表示 xlsx，1 表示 docx，2 表示 pptx
        System.out.println(file.getName());
        System.out.println(type);
        return pictureRecognitionTextService.toDocument(file,type);
    }

    // 图片文字识别转字体接口
    @PostMapping("/image/universal")
    public Result<String> UniversalFont(@RequestParam("file") MultipartFile file) {
        System.out.println(file.getName());
        return pictureRecognitionTextService.toFont(file);
    }

    // 图片文字识别转语言接口
    @PostMapping("/image/textRecognition")
    public Result<String> recognizeTextFromImage(@RequestParam("file") MultipartFile file,
                                                 @RequestParam("language") String language) throws Exception {
        System.out.println(file.getName());
        System.out.println(language);
        return pictureRecognitionTextService.toLanguage(file,language);
    }

    // PPT生成接口
    @PostMapping("/Ppt/generate")
    public Result<String> generatePPT(@RequestBody String description) {
        System.out.println("收到请求");
        try {
            String result = pptService.generatePPT(description);
            System.out.println(Result.success(result));
            return Result.success(result);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail("生成PPT失败: " + e.getMessage());
        }
    }
}
